/**
 * 
 */
package neptune.web.client.controller;

import neptune.web.client.services.experiment.ExperimentService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentSummaryView;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.dialogs.PropertiesDialog;
import neptune.web.client.view.components.dialogs.PropertiesDialogListener;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class RemoveExperimentDialogController implements
		PropertiesDialogListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see neptune.web.client.view.components.dialogs.PropertiesDialogListener#okPressed(java.lang.String[][],
	 *      neptune.web.client.view.components.dialogs.PropertiesDialog)
	 */
	public void okPressed(final String[][] properties,
			final PropertiesDialog sender) {

		ExperimentService.Util.getInstance().deleteExperiment(properties[0][1],
				new AsyncCallback<Boolean>() {

					public void onSuccess(Boolean arg0)
					{
						String message;
						if (arg0.booleanValue() )
						{
							message = "Experiment " + properties[0][1]
							        								+ " successfully deleted";
						}
						else
						{
							message = "Unable to delete experiment " + properties[0][1]+ " !";
						} 

						ConsoleManager.addConsoleMessage(message, false);

						sender.showMessage(message);

						SuperView operative = (SuperView) ViewManager
								.getViewManager().getViewByName(
										OperativeView.DEFAULT_TOKEN);
						ExperimentSummaryView expView = (ExperimentSummaryView) operative
								.getViewByName(ExperimentSummaryView.DEFAULT_TOKEN);
						expView.reset();
						expView.update();
					}

					public void onFailure(Throwable arg0) {
						String message = "Impossible to delete experiment "
								+ properties[0][1] + " Cause: "
								+ arg0.getMessage();

						ConsoleManager.addConsoleMessage(message, true);

						sender.showMessage(message);
					}

				});

	}

}
